﻿// Copyright: 2012 
// Author: Minh Vu - YoungJ
// File name: MyBase.cs
// Solution: getlink
// Project: MediaFire
// Time: 2:33 PM 02/05/2012

using System;
using System.Net;

namespace MediaFire
{
    public class FileDownload : MyBase
    {
        public string QuickKey { get; set; }
        public string Filename { get; set; }
        public string Size { get; set; }
        public string PasswordProtected { get; set; }

        public string Link
        {
            get { return "http://www.mediafire.com/?" + QuickKey; }
        }

        public string StrSize
        {
            get
            {

                return string.IsNullOrEmpty(Size) ? string.Empty : string.Format("{0:0,0 MB}", Double.Parse(Size) / Math.Pow(1024, 2));
            }
        }

        #region My property changed

        public string LinkDownload
        {
            get { return _linkDownload; }
            set
            {
                if (string.IsNullOrEmpty(value)) return;
                _linkDownload = value;
                OnPropertyChanged("LinkDownload");
            }
        }

        private string _linkDownload;
        #endregion

        public void GetLinkDownload(string url)
        {
            var client = new WebClient();
            url = url.Replace("download.php", string.Empty);
            var index = url.LastIndexOf("&", StringComparison.Ordinal);
            if (index > 0)
            {
                var i = 1;
                var pass = "downloadp=" + url.Substring(index + 6);
                url = url.Substring(0, index);
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                client.UploadStringCompleted += client_UploadStringCompleted;
                client.UploadProgressChanged += (sender, e) => { Percent = i += 2; };
                client.UploadStringAsync(new Uri(url, UriKind.Absolute), "POST", pass);

            }
            else
            {
                client.DownloadStringCompleted += client_DownloadStringCompleted;
                client.DownloadProgressChanged += (sender, e) => { Percent = e.ProgressPercentage; };
                client.DownloadStringAsync(new Uri(url, UriKind.Absolute));
            }
        }

        void client_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Error = e.Error.Message;
                return;
            }
            FindLink(e.Result);
            Percent = 100;
        }



        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                Error = e.Error.Message;
                return;
            }
            FindLink(e.Result);
        }

        void FindLink(string html)
        {
            var document = new HtmlAgilityPack.HtmlDocument();
            document.LoadHtml(html);
            var downloadLinkNode =
                document.DocumentNode.SelectSingleNode("//div[@class='download_link']//a[starts-with(@href,'http://')]");
            if (downloadLinkNode == null)
            {
                Error = "Can not find link download";
                return;
            }
            LinkDownload = downloadLinkNode.Attributes["href"].Value;
        }

    }
}
